import settlementApi from '@/api/settlement/settlement'
import attrKeyApi from '@/api/shop/attrKey'
import attrValApi from '@/api/shop/attrVal'
import { getApiUrl } from '@/utils/utils'
import permission from '@/directive/permission/index.js'
export default {
  directives: { permission },
  data() {
    return {
      list: [],
      formTitle: "设置核销比",
      formVisible: false,
      form: {
        goodsId: 0,
        goodsName: "",
        rate: 0,
        count: 0,
        price: 0,
        totalPrice: 0,

      },
      goodsCreatList: [],
      goodsCreatSelect: 0,
      itemIds: [],
      writeOffTotalString: "￥0.00",
      oldTotalString: "￥0.00",
      isShowString: false,
    }
  },
  watch: {
    goodsCreatSelect(val, oldVal) {
      //console.log('筛选框值变动', val, oldVal);
      this.fetchData(val)
    }
  },
  computed: {
    willWriteOffNum: function () {
      return this.fmoney(this.form.totalPrice * this.form.rate, 0)
    },
    formTotalPrice: function () {
      return this.fmoney(this.form.totalPrice, 0)
    },
    formPrice: function () {
      return this.fmoney(this.form.price, 0)
    },
  },
  created() {
    this.init()
  },
  methods: {

    init() {
      this.fetchData(0)
      this.getGoodsCreatList()
    },
    fetchData(goodsCreatId) {
      // if (goodsCreatId == 0) {
      //   let thisUserId = this.$store.state.user.profile.id
      //   for (let i = 0; i < this.goodsCreatList.length; i++) {
      //     const e = this.goodsCreatList[i];
      //     if (e.id == thisUserId) {
      //       this.goodsCreatSelect = e.id
      //       goodsCreatId = e.id
      //       break
      //     } else {
      //       goodsCreatId = 0
      //     }
      //   }
      // }

      this.listLoading = true
      let tmpReqData = {
        "goodsCreat": goodsCreatId
      }
      settlementApi.queryUnWriteOffList(tmpReqData).then(response => {
        // console.log("未核销列表相应数据：", response);
        this.list = response.data
        this.listLoading = false
        this.total = response.data.length
      })
    },
    updateWriteOffRateButton() {
      //console.log("goodsId", this.goodsId);
      let param = {
        goodsId: this.form.goodsId,
        rate: this.form.rate,
      }
      settlementApi.setWriteOffRate(param).then(response => {
        //console.log("提交设置核销比", response);

      })
    },
    writeOffButton() {
      if (this.itemIds.length == 0) {
        this.$message({
          message: '请勾选要核销的数据',
          type: 'error'
        })
        return
      }
      let param = {
        itemIds: this.itemIds.join(","),
      }
      settlementApi.writeOff(param).then(response => {
        //console.log("提交设置核销比", response);
        if (response.code == 20000) {
          this.fetchData(this.goodsCreatSelect)
        } else {
          //console.log("核销失败", response);
        }
      })
    },
    selectGoods(row, index) {
      if (row.writeOffRate == null) {
        return false
      } else {
        return true
      }
    },
    handleSelectionChange(val) {
      this.itemIds = []
      if (val.length > 0) {
        this.isShowString = true
        this.writeOffTotalString = "￥0.00"
        this.oldTotalString = "￥0.00"
      } else {
        this.isShowString = false
      }
      let newString = 0
      let oldString = 0
      for (let i = 0; i < val.length; i++) {
        const e = val[i];
        newString = newString + e.writeOffMoney
        oldString = oldString + e.totalPrice
        this.itemIds.push(e.id)
      }
      this.writeOffTotalString = "￥" + this.fmoney(newString / 100)
      this.oldTotalString = "￥" + this.fmoney(oldString / 100)

    },
    oninput(num) {
      var str = num.toString()
      var len1 = str.substr(0, 1)
      var len2 = str.substr(1, 1)
      //如果第一位是0，第二位不是点，就用数字把点替换掉
      if (str.length > 1 && len1 == 0 && len2 != ".") {
        str = str.substr(1, 1)
      }
      //第一位不能是.
      if (len1 == ".") {
        str = ""
      }
      //限制只能输入一个小数点
      if (str.indexOf(".") != -1) {
        var str_ = str.substr(str.indexOf(".") + 1)
        if (str_.indexOf(".") != -1) {
          str = str.substr(0, str.indexOf(".") + str_.indexOf(".") + 1)
        }
      }
      //正则替换
      str = str.replace(/[^\d^\.]+/g, '') // 保留数字和小数点
      str = str.replace(/\.\d\d\d$/, '') // 小数点后只能输两位
      return Number(str)
    },
    fmoney(s, n) {
      /*
       * 参数说明：
       * s：要格式化的数字
       * n：保留几位小数
       * */
      n = n > 0 && n <= 20 ? n : 2;
      s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
      var l = s.split(".")[0].split("").reverse(),
        r = s.split(".")[1];
      let t = "";
      for (let i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
      }
      return t.split("").reverse().join("") + "." + r;
    },
    writeOffBotton() {
      //console.log("点击核销按钮");
    },
    getGoodsCreatList() {
      settlementApi.getSysUser().then(response => {
        let a = {
          id: 0,
          nameAndAccount: "全部"
        }
        this.goodsCreatList.push(a)
        //console.log("获取创建者ID", response);
        for (let i = 0; i < response.data.length; i++) {
          const e = response.data[i];
          let tmp = {
            id: e.id,
            nameAndAccount: e.nameAndAccount,
            name: e.name,
          }
          this.goodsCreatList.push(tmp)
        }
        this.goodsCreatSelect = 0
      })
    },
    formatCreat: function (row, cellValue) {
      // //console.log(JSON.stringify(row))
      let goodsCreat = row.goodsCreat
      let userList = this.goodsCreatList
      for (let item in userList) {
        let id = userList[item].id
        if (id == goodsCreat) {
          return userList[item].name
        }
      }
    },
  }
}
